﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AjaxControlToolkit;
using FrameworkManapro.Utilities;
using GMAC.ReportingSystem.Business.Entities.Configuration;

namespace GMAC.ReportingSystem.WebControls
{
    [ToolboxData("<{0}:ConfigurationControl runat=server></{0}:ConfigurationControl>")]
    public class ConfigurationControl : CompositeControl
    {
        private Table table = new Table();
        private ConfigurationParameterCollection dataSource;
        private TableItemStyle rowStyle = null;
        private TableItemStyle alternatingRowStyle = null;
        private TableItemStyle descriptionRowStyle = null;
        private TableItemStyle alternatingDescriptionRowStyle = null;

        public ConfigurationParameterCollection DataSource
        {
            get
            {
                return ViewState["DataSource"] as ConfigurationParameterCollection;
            }
            set
            {
                this.dataSource = value;
            }
        }

        public TableRowCollection Rows
        {
            get { return table.Rows; }
        }

        public string CalendarCssClass
        {
            get
            {
                return ViewState["CalendarCssClass"] as string;
            }
            set
            {
                ViewState["CalendarCssClass"] = value;
            }
        }

        [PersistenceMode(PersistenceMode.InnerProperty)]
        public virtual TableItemStyle RowStyle
        {
            get
            {
                if (this.rowStyle == null)
                    this.rowStyle = new TableItemStyle();
                return this.rowStyle;
            }
        }

        [PersistenceMode(PersistenceMode.InnerProperty)]
        public virtual TableItemStyle AlternatingRowStyle
        {
            get
            {
                if (this.alternatingRowStyle == null)
                    this.alternatingRowStyle = new TableItemStyle();
                return this.alternatingRowStyle;
            }
        }

        [PersistenceMode(PersistenceMode.InnerProperty)]
        public virtual TableItemStyle DescriptionRowStyle
        {
            get
            {
                if (this.descriptionRowStyle == null)
                    this.descriptionRowStyle = new TableItemStyle();
                return this.descriptionRowStyle;
            }
        }

        [PersistenceMode(PersistenceMode.InnerProperty)]
        public virtual TableItemStyle AlternatingDescriptionRowStyle
        {
            get
            {
                if (this.alternatingDescriptionRowStyle == null)
                    this.alternatingDescriptionRowStyle = new TableItemStyle();
                return this.alternatingDescriptionRowStyle;
            }
        }

        public override void DataBind()
        {
            base.OnDataBinding(EventArgs.Empty);
            Controls.Clear();
            ClearChildViewState();
            TrackViewState();

            CreateControlHierarchy(true);
            ChildControlsCreated = true;
        }

        protected override void CreateChildControls()
        {
            this.Controls.Clear();
            if (ViewState["DataSource"] != null)
                CreateControlHierarchy(false);
        }

        protected override void Render(HtmlTextWriter writer)
        {
            PrepareHierarchyForRendering();
            string includeCSSTemplate = "<style type=\"text/css\">@import url('{0}');</style>";
            string includeLocation = Page.ClientScript.GetWebResourceUrl(this.GetType(), "TestWebControlLibrary.Stylesheet.css");
            writer.WriteLine(string.Format(includeCSSTemplate, includeLocation));
            RenderContents(writer);
        }

        private void CreateControlHierarchy(bool useDataSource)
        {
            ConfigurationParameterCollection collection;
            int count = 0;
            this.table.Rows.Clear();

            if (useDataSource)
            {
                collection = this.dataSource;
                ViewState["DataSource"] = collection;
            }
            else
            {
                collection = ViewState["DataSource"] as ConfigurationParameterCollection;
            }

            if (collection != null)
            {
                ConfigurationItem item;
                foreach (ConfigurationParameter cv in collection)
                {
                    item = new ConfigurationItem(cv, count++, this.CalendarCssClass);
                    item.CssClass = this.CssClass;
                    this.table.Rows.Add(item);
                }
                this.Controls.Add(this.table);
            }
        }

        private void PrepareHierarchyForRendering()
        {
            if (this.Controls.Count == 0) return;

            Table outerTable = this.Controls[0] as Table;
            if (outerTable == null) return;

            outerTable.CopyBaseAttributes(this);
            outerTable.ApplyStyle(this.ControlStyle);

            if (this.rowStyle == null && this.alternatingRowStyle == null) return;

            TableItemStyle mergedRowStyle = null;
            if (this.alternatingRowStyle != null)
            {
                mergedRowStyle = new TableItemStyle();
                mergedRowStyle.CopyFrom(this.rowStyle);
                mergedRowStyle.CopyFrom(this.alternatingRowStyle);
            }
            else mergedRowStyle = this.rowStyle;


            TableItemStyle tempDescriptionStyle = null;
            TableItemStyle mergedDescriptionStyle = null;
            if (this.descriptionRowStyle == null)
            {
                tempDescriptionStyle = this.rowStyle;
                mergedDescriptionStyle = mergedRowStyle;
            }
            else
            {
                tempDescriptionStyle = this.descriptionRowStyle;
                if (this.alternatingDescriptionRowStyle != null)
                {
                    mergedDescriptionStyle = new TableItemStyle();
                    mergedDescriptionStyle.CopyFrom(this.descriptionRowStyle);
                    mergedDescriptionStyle.CopyFrom(this.alternatingDescriptionRowStyle);
                }
                else mergedDescriptionStyle = tempDescriptionStyle;
            }

            bool isAlternate = false;

            foreach (ConfigurationItem row in outerTable.Rows)
            {
                if (!isAlternate)
                {
                    row.MergeStyle(rowStyle);
                    row.Cells[0].MergeStyle(tempDescriptionStyle);
                }
                else
                {
                    row.MergeStyle(mergedRowStyle);
                    row.Cells[0].MergeStyle(mergedDescriptionStyle);
                }
                isAlternate = !isAlternate;
            }
        }

    }

    public class ConfigurationItem : TableRow, System.Web.UI.IDataItemContainer
    {
        private ConfigurationParameter data;
        private int index;
        private string calendarCssClass;

        public ConfigurationItem(ConfigurationParameter data, int index, string calendarCssClass)
        {
            this.data = data;
            this.index = index;
            this.calendarCssClass = calendarCssClass;
        }

        #region IDataItemContainer Members

        public object DataItem
        {
            get { return this.data; }
        }

        public int DataItemIndex
        {
            get { return this.index; }
        }

        public int DisplayIndex
        {
            get { return this.index; }
        }

        #endregion

        protected override void CreateChildControls()
        {
            if (this.data != null)
            {
                TableCell tc1 = new TableCell();
                tc1.Text = this.data.Description;
                tc1.Width = 240;
                //tc1.ToolTip = this.data.Parameter.Description;
                this.Cells.Add(tc1);

                TableCell tc2 = new TableCell();
                this.Cells.Add(tc2);

                switch (this.data.DataType.ToString())
                {
                    case "System.String":
                        TextBox txtString = new TextBox();
                        txtString.ID = "txtString";
                        txtString.Text = this.data.Value.ToString();
                        txtString.TextChanged += new EventHandler(txtString_TextChanged);
                        txtString.CssClass = this.CssClass;

                        if (this.data.Length.HasValue)
                            txtString.MaxLength = this.data.Length.Value;
                        else
                            txtString.MaxLength = 200;
                        txtString.SkinID = "TextBox12pxMultiLarge";
                        txtString.Width = 650;
                        txtString.TextMode = TextBoxMode.MultiLine;
                        tc2.Controls.Add(txtString);
                        RequiredFieldValidator rfvString = new RequiredFieldValidator();
                        rfvString.ID = "rfvString";
                        rfvString.ControlToValidate = "txtString";
                        rfvString.Text = "*";
                        ValidatorCalloutExtender vceString = new ValidatorCalloutExtender();
                        vceString.ID = "vceString";
                        vceString.TargetControlID = "rfvString";
                        tc2.Controls.Add(rfvString);
                        tc2.Controls.Add(vceString);
                        break;
                    case "System.Byte":
                    case "System.Int32":
                    case "System.Int16":
                    case "System.Int64":
                        TextBox txtInteger = new TextBox();
                        txtInteger.ID = "txtInteger";
                        txtInteger.Text = this.data.Value.ToString();
                        txtInteger.TextChanged += new EventHandler(txtNumeric_TextChanged);
                        txtInteger.CssClass = this.CssClass;
                        txtInteger.Width = 100;
                        FilteredTextBoxExtender filterInteger = new FilteredTextBoxExtender();
                        filterInteger.ID = "filterInteger";
                        filterInteger.TargetControlID = "txtInteger";
                        filterInteger.FilterType = FilterTypes.Numbers;
                        filterInteger.FilterMode = FilterModes.ValidChars;
                        if (this.data.Length.HasValue)
                            txtInteger.MaxLength = this.data.Length.Value;
                        else if (this.data.DataType.ToString() == "System.Int16")
                            txtInteger.MaxLength = 3;
                        else if (this.data.DataType.ToString() == "System.Int16")
                            txtInteger.MaxLength = 5;
                        else if (this.data.DataType.ToString() == "System.Int32")
                            txtInteger.MaxLength = 10;
                        else if (this.data.DataType.ToString() == "System.Int64")
                            txtInteger.MaxLength = 20;
                        txtInteger.SkinID = "TextBox12px";
                        tc2.Controls.Add(txtInteger);
                        tc2.Controls.Add(filterInteger);

                        RequiredFieldValidator rfvInteger = new RequiredFieldValidator();
                        rfvInteger.ID = "rfvInteger";
                        rfvInteger.ControlToValidate = "txtInteger";
                        rfvInteger.Text = "*";
                        tc2.Controls.Add(rfvInteger);

                        ValidatorCalloutExtender vceInteger = new ValidatorCalloutExtender();
                        vceInteger.ID = "vceInteger";
                        vceInteger.TargetControlID = "rfvInteger";
                        tc2.Controls.Add(vceInteger);

                        if (this.data.MinValue != null && this.data.MaxValue != null)
                        {
                            RangeValidator rvInteger = new RangeValidator();
                            rvInteger.ID = "rvInteger";
                            rvInteger.ControlToValidate = "txtInteger";
                            rvInteger.Type = ValidationDataType.Integer;
                            rvInteger.MinimumValue = this.data.MinValue.ToString();
                            rvInteger.MaximumValue = this.data.MaxValue.ToString();
                            rvInteger.ErrorMessage = String.Format("The value must be between {0} and {1}", this.data.MinValue, this.data.MaxValue);
                            rvInteger.Text = "*";
                            rvInteger.Display = ValidatorDisplay.Dynamic;
                            tc2.Controls.Add(rvInteger);

                            ValidatorCalloutExtender vceInteger2 = new ValidatorCalloutExtender();
                            vceInteger2.ID = "vceInteger2";
                            vceInteger2.TargetControlID = "rvInteger";
                            tc2.Controls.Add(vceInteger2);
                        }

                        break;
                    case "System.Single":
                    case "System.Double":
                    case "System.Decimal":
                        TextBox txtDecimal = new TextBox();
                        txtDecimal.ID = "txtDecimal";
                        if (this.data.Length.HasValue)
                            txtDecimal.MaxLength = this.data.Length.Value;
                        else
                            txtDecimal.MaxLength = 20;

                        txtDecimal.Text = this.data.Value.ToString();
                        txtDecimal.TextChanged += new EventHandler(txtNumeric_TextChanged);
                        txtDecimal.CssClass = this.CssClass;
                        txtDecimal.Width = 100;
                        txtDecimal.SkinID = "TextBox12px";
                        FilteredTextBoxExtender filterDecimal = new FilteredTextBoxExtender();
                        filterDecimal.ID = "filterInteger";
                        filterDecimal.TargetControlID = "txtDecimal";
                        filterDecimal.FilterType = FilterTypes.Custom | FilterTypes.Numbers;
                        filterDecimal.FilterMode = FilterModes.ValidChars;
                        filterDecimal.ValidChars = ".,";
                        CompareValidator cvDecimal = new CompareValidator();
                        cvDecimal.ID = "cvDecimal";
                        cvDecimal.SkinID = "cvDecimasl";
                        cvDecimal.ControlToValidate = "txtDecimal";
                        cvDecimal.Operator = ValidationCompareOperator.NotEqual;
                        cvDecimal.ValueToCompare = "-1";
                        cvDecimal.Type = ValidationDataType.Double;
                        cvDecimal.Text = "*";
                        tc2.Controls.Add(txtDecimal);
                        tc2.Controls.Add(filterDecimal);
                        tc2.Controls.Add(cvDecimal);

                        RequiredFieldValidator rfvDecimal = new RequiredFieldValidator();
                        rfvDecimal.ID = "rfvDecimal";
                        rfvDecimal.ControlToValidate = "txtDecimal";
                        rfvDecimal.Text = "*";
                        ValidatorCalloutExtender vceDecimal = new ValidatorCalloutExtender();
                        vceDecimal.ID = "vceDecimal";
                        vceDecimal.TargetControlID = "rfvDecimal";
                        ValidatorCalloutExtender vceDecimal2 = new ValidatorCalloutExtender();
                        vceDecimal2.ID = "vceDecimal2";
                        vceDecimal2.TargetControlID = "cvDecimal";

                        tc2.Controls.Add(rfvDecimal);
                        tc2.Controls.Add(vceDecimal);
                        tc2.Controls.Add(vceDecimal2);

                        if (this.data.MinValue != null && this.data.MaxValue != null)
                        {
                            RangeValidator rvDecimal = new RangeValidator();
                            rvDecimal.ID = "rvDecimal";
                            rvDecimal.ControlToValidate = "txtDecimal";
                            rvDecimal.Type = ValidationDataType.Double;
                            rvDecimal.MinimumValue = this.data.MinValue.ToString();
                            rvDecimal.MaximumValue = this.data.MaxValue.ToString();
                            rvDecimal.ErrorMessage = String.Format("The value must be between {0} and {1}", this.data.MinValue, this.data.MaxValue);
                            rvDecimal.Text = "*";
                            rvDecimal.Display = ValidatorDisplay.Dynamic;
                            tc2.Controls.Add(rvDecimal);

                            ValidatorCalloutExtender vceDecimal3 = new ValidatorCalloutExtender();
                            vceDecimal3.ID = "vceDecimal3";
                            vceDecimal3.TargetControlID = "rvDecimal";
                            tc2.Controls.Add(vceDecimal3);
                        }

                        break;
                    case "System.DateTime":
                        TextBox txtDateTime = new TextBox();
                        txtDateTime.ID = "txtDateTime";
                        txtDateTime.MaxLength = 10;
                        txtDateTime.Width = 100;
                        txtDateTime.CssClass = this.CssClass;
                        txtDateTime.TextChanged += new EventHandler(txtDateTime_TextChanged);
                        txtDateTime.SkinID = "TextBox12px";
                        CalendarExtender calendar = new CalendarExtender();
                        calendar.ID = "calendar";
                        calendar.TargetControlID = "txtDateTime";
                        calendar.FirstDayOfWeek = FirstDayOfWeek.Monday;
                        calendar.PopupPosition = CalendarPosition.BottomLeft;
                        if (string.IsNullOrEmpty(this.calendarCssClass))
                            calendar.CssClass = "CustomCalendar";
                        else
                            calendar.CssClass = this.calendarCssClass;
                        MaskedEditExtender maskDateTime = new MaskedEditExtender();
                        maskDateTime.ID = "maskDateTime";
                        maskDateTime.TargetControlID = "txtDateTime";
                        maskDateTime.Mask = "99/99/9999";
                        maskDateTime.MaskType = MaskedEditType.Date;
                        MaskedEditValidator maskValDateTime = new MaskedEditValidator();
                        maskValDateTime.ID = "maskValDateTime";
                        maskValDateTime.Display = ValidatorDisplay.Dynamic;
                        maskValDateTime.InvalidValueMessage = "*";
                        maskValDateTime.ToolTip = "Invalid Date";
                        maskValDateTime.ControlExtender = "maskDateTime";
                        maskValDateTime.ControlToValidate = "txtDateTime";
                        MaskedEditValidator maskValDateTime2 = new MaskedEditValidator();
                        maskValDateTime.ID = "maskValDateTime2";
                        maskValDateTime.Display = ValidatorDisplay.None;
                        maskValDateTime.InvalidValueMessage = "Invalid Date";
                        maskValDateTime.ControlExtender = "maskDateTime";
                        maskValDateTime.ControlToValidate = "txtDateTime";

                        tc2.Controls.Add(txtDateTime);
                        tc2.Controls.Add(calendar);
                        tc2.Controls.Add(maskDateTime);
                        tc2.Controls.Add(maskValDateTime);
                        tc2.Controls.Add(maskValDateTime2);

                        RequiredFieldValidator rfvDateTime = new RequiredFieldValidator();
                        rfvDateTime.ID = "rfvDateTime";
                        rfvDateTime.ControlToValidate = "txtDateTime";
                        rfvDateTime.Text = "*";
                        ValidatorCalloutExtender vceDateTime = new ValidatorCalloutExtender();
                        vceDateTime.ID = "vceDateTime";
                        vceDateTime.TargetControlID = "rfvDateTime";
                        ValidatorCalloutExtender vceDateTime2 = new ValidatorCalloutExtender();
                        vceDateTime2.ID = "vceDateTime2";
                        vceDateTime2.TargetControlID = "maskValDateTime2";

                        tc2.Controls.Add(rfvDateTime);
                        tc2.Controls.Add(vceDateTime);
                        tc2.Controls.Add(vceDateTime2);

                        if (this.data.MinValue != null && this.data.MaxValue != null)
                        {
                            RangeValidator rvDateTime = new RangeValidator();
                            rvDateTime.ID = "rvDateTime";
                            rvDateTime.ControlToValidate = "txtDateTime";
                            rvDateTime.Type = ValidationDataType.Date;
                            rvDateTime.MinimumValue = this.data.MinValue.ToString();
                            rvDateTime.MaximumValue = this.data.MaxValue.ToString();
                            rvDateTime.ErrorMessage = String.Format("The value must be between {0} and {1}", this.data.MinValue, this.data.MaxValue);
                            rvDateTime.Text = "*";
                            rvDateTime.Display = ValidatorDisplay.Dynamic;

                            ValidatorCalloutExtender vceDateTime3 = new ValidatorCalloutExtender();
                            vceDateTime3.ID = "vceDateTime3";
                            vceDateTime3.TargetControlID = "rvDateTime";
                            tc2.Controls.Add(rvDateTime);
                            tc2.Controls.Add(vceDateTime3);
                        }

                        break;
                    case "System.TimeSpan":
                        TextBox txtTime = new TextBox();
                        TimeSpan time = TimeSpan.Parse(this.data.Value.ToString());
                        if (time.Hours > 12)
                        {
                            txtTime.Text = (time.Hours - 12).ToString("00") + ":" + time.Minutes.ToString("00") + " p.m.";
                        }
                        else
                        {
                            txtTime.Text = time.Hours.ToString("00") + ":" + time.Minutes.ToString("00") + " a.m.";
                        }
                        txtTime.ID = "txtTime";
                        txtTime.CssClass = this.CssClass;
                        txtTime.Width = 100;
                        txtTime.TextChanged += new EventHandler(txtTime_TextChanged);
                        MaskedEditExtender maskTime = new MaskedEditExtender();
                        maskTime.ID = "maskTime";
                        maskTime.TargetControlID = "txtTime";
                        maskTime.MaskType = MaskedEditType.Time;
                        maskTime.Mask = "99:99";
                        maskTime.AcceptAMPM = true;
                        MaskedEditValidator maskValTime = new MaskedEditValidator();
                        maskValTime.ControlExtender = "maskTime";
                        maskValTime.ControlToValidate = "txtTime";
                        maskValTime.Display = ValidatorDisplay.Dynamic;
                        maskValTime.InvalidValueMessage = "*";
                        maskValTime.ToolTip = "Invalid Value";
                        MaskedEditValidator maskValTime2 = new MaskedEditValidator();
                        maskValTime2.ControlExtender = "maskTime";
                        maskValTime2.ControlToValidate = "txtTime";
                        maskValTime2.Display = ValidatorDisplay.None;
                        maskValTime.InvalidValueMessage = "Invalid Value";
                        tc2.Controls.Add(txtTime);
                        tc2.Controls.Add(maskTime);
                        tc2.Controls.Add(maskValTime);
                        tc2.Controls.Add(maskValTime2);

                        RequiredFieldValidator rfvTime = new RequiredFieldValidator();
                        rfvTime.ID = "rfvTime";
                        rfvTime.ControlToValidate = "txtDateTime";
                        rfvTime.Text = "*";
                        ValidatorCalloutExtender vceTime = new ValidatorCalloutExtender();
                        vceTime.ID = "vceTime";
                        vceTime.TargetControlID = "rfvTime";
                        ValidatorCalloutExtender vceTime2 = new ValidatorCalloutExtender();
                        vceTime2.ID = "vceDateTime2";
                        vceTime2.TargetControlID = "maskValTime2";

                        tc2.Controls.Add(rfvTime);
                        tc2.Controls.Add(vceTime);
                        tc2.Controls.Add(vceTime2);

                        break;
                    case "System.Boolean":
                        CheckBox chkBoolean = new CheckBox();
                        chkBoolean.ID = "chkBoolean";
                        chkBoolean.Checked = (bool)this.data.Value;
                        chkBoolean.CheckedChanged += new EventHandler(chkBoolean_CheckedChanged);
                        chkBoolean.CssClass = this.CssClass;
                        tc2.Controls.Add(chkBoolean);
                        break;
                    default:
                        Type type = this.data.DataType;
                        if (type.IsEnum)
                        {
                            FlagsAttribute[] flags = type.GetCustomAttributes(typeof(FlagsAttribute), false) as FlagsAttribute[];
                            if (flags != null && flags.Length != 0)
                            {
                                CheckBoxList chklstEnumFlags = new CheckBoxList();
                                chklstEnumFlags.RepeatDirection = System.Web.UI.WebControls.RepeatDirection.Horizontal;
                                chklstEnumFlags.RepeatLayout = RepeatLayout.Flow;
                                chklstEnumFlags.Width = new Unit("70%");
                                chklstEnumFlags.CssClass = this.CssClass;
                                long value, flag;
                                value = Convert.ToInt64(this.data.Value);
                                foreach (Enum d in Enum.GetValues(type))
                                {
                                    chklstEnumFlags.Items.Add(new ListItem(d.ToDescription(), Convert.ChangeType(d, Enum.GetUnderlyingType(type)).ToString()));
                                    flag = Convert.ToInt64(d);
                                    if ((value & flag) == flag)
                                        chklstEnumFlags.Items[chklstEnumFlags.Items.Count - 1].Selected = true;
                                }

                                chklstEnumFlags.SelectedIndexChanged += new EventHandler(chklstEnumFlags_SelectedIndexChanged);
                                tc2.Controls.Add(chklstEnumFlags);
                            }
                            else
                            {
                                System.Collections.IDictionary keyValues = FrameworkManapro.Utilities.EnumExtensions.ToKeyValueCollection(type);
                                DropDownList ddlEnumOptions = new DropDownList();
                                ddlEnumOptions.DataSource = keyValues;
                                ddlEnumOptions.DataTextField = "Value";
                                ddlEnumOptions.DataValueField = "Key";
                                ddlEnumOptions.DataBind();
                                ddlEnumOptions.CssClass = this.CssClass;
                                tc2.Controls.Add(ddlEnumOptions);
                            }
                        }
                        break;
                }
            }
        }

        void txtTime_TextChanged(object sender, EventArgs e)
        {
            TextBox txtTime = sender as TextBox;
            string text = txtTime.Text;
            bool am = (text.IndexOf("a.m", StringComparison.OrdinalIgnoreCase) != -1);
            int hours, minutes;
            hours = int.Parse(text.Substring(0, 2));
            minutes = int.Parse(text.Substring(3, 2));
            TimeSpan time;
            if (am)
                time = new TimeSpan(hours, minutes, 0);
            else
                time = new TimeSpan(hours + 12, minutes, 0);
            this.data.Value = time;
        }

        void txtDateTime_TextChanged(object sender, EventArgs e)
        {
            TextBox txtDateTime = (TextBox)sender;
            DateTime t;
            DateTime.TryParse(txtDateTime.Text, out t);
            if (t != DateTime.MinValue)
                this.data.Value = t;
            else
                txtDateTime.Text = this.data.Value.ToString();
        }

        void txtNumeric_TextChanged(object sender, EventArgs e)
        {
            TextBox txtNumeric = (TextBox)sender;
            try
            {
                this.data.Value = Convert.ChangeType(txtNumeric.Text, this.data.DataType);
            }
            catch
            {
                txtNumeric.Text = this.data.Value.ToString();
            }
        }

        void chklstEnumFlags_SelectedIndexChanged(object sender, EventArgs e)
        {
            CheckBoxList chklstEnumFlags = sender as CheckBoxList;
            long value = 0;
            foreach (ListItem item in chklstEnumFlags.Items)
            {
                if (item.Selected)
                    value += long.Parse(item.Value);
            }
            this.data.Value = value.ToString();
        }

        void chkBoolean_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox chkBoolean = sender as CheckBox;
            this.data.Value = chkBoolean.Checked;
        }

        void txtString_TextChanged(object sender, EventArgs e)
        {
            TextBox txtString = (TextBox)sender;
            this.data.Value = txtString.Text;
        }
    }
}
